#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::Maipu::ShowSystemPower < Aio::Module::Cmd::Maipu
	include Aio::Module

	def initialize
		super({
			:cmd_full		=> "show system power",
			:cmd_short	=> "sh sys po",
			:author			=> "Elin",
			:description	=> "This is Maipu Command# show system power",
			:ranking		=> Ranking_1,
			:platform		=> "all",
			:benchmark	=> {
				:power_status		=> ["==", "NORMAL"],
				:power_alarm		=> ["<", 1]
				}
		})
	end

	def parse
		cont = self.context.dup
		useful[:power] = {}
		
		cont.readline_range_loop(/System Power Information/, reg_blank) do |cont_layer|
			parse_power(cont_layer)
		end
	end

	def parse_power(context)
		power = {}
		power_id = nil
		context.readline_match_block(/System Power Information\(Power (?<power_id>\d+) - (?<state>.*)\)/) do |block|
			power_id = block[:power_id]
			useful[:power]["slot_#{power_id}"] = power
			block.update(power, :power_id)
			block.update(power, :state)
		end

		if power[:state] =~ /ONLINE/
			context.readline_match_block(/Status: (?<power_status>.*)/) do |block|
				block.warning_serious(power, :power_status, self)
			end
			context.readline_match_block(/Last-Alarm: (?<power_alarm>\d+)/) do |block|
				block.warning_serious(power, :power_alarm, self)
			end
			context.readline_match_block(/Serial No\.: (?<sn>.*)/) do |block|
				block.update(power, :sn)
			end
			context.readline_match_block(/Description: (?<description>.*)/) do |block|
				block.update(power, :description)
			end
		end

		context.readline_match_block(/STATISTICS: (?<in>\d+) IN, (?<out>\d+) OUT, (?<in_error>\d+) IERR, (?<out_error>\d+) OERR/) do |block|
			stat = {}
			power[:statistics] = stat
			block.update(stat, :in)
			block.update(stat, :out)
			block.update(stat, :in_error)
			block.update(stat, :out_error)
		end
	end
end
